<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *音色
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'音色列表'),
	//array('op'=>'edit','title'=>'添加音色'),
	//array('op'=>'listadd','title'=>'批量导入音色表'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['agetype'])){
		$params['agetype']=intval($_RQ['agetype']);
		$return['params']['agetype']=$_RQ['agetype'];
	}
	if(!empty($_RQ['language'])){
		$params['language like']='%'.trim($_RQ['language']).'%';
		$return['params']['language']=$_RQ['language'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aiagent_voice',$params,array($pindex,$psize),$total,$fields,'','ishot desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if(!$item['logo']){
				$num = mt_rand(1, 6);
				$item['logo']='../addons/jy_aiagent/resource/images/voice'.$num.'.png';
				pdo_update('jy_aiagent_voice',array('logo'=>$item['logo']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$tips='注意voice参数值是区分声音的重要依据：声音类型:需要点击同步音色获取！';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tips,'edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'language','title'=>'语言','type'=>'radio','options'=>'yuyuantypes'),
			array('name'=>'logo','title'=>'头像','type'=>'image'),
			array('name'=>'title','title'=>'音色名称','type'=>'string'),
			array('name'=>'voiceid','title'=>'voice参数值','type'=>'string'),
			array('name'=>'sex','title'=>'男/女','type'=>'radio','options'=>'sex'),
			//array('name'=>'yintype','title'=>'声音类型','type'=>'string'),
			array('name'=>'demo','title'=>'试听','type'=>'audio'),
			array('name'=>'ishot','title'=>'推荐','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/voice/updata/','title'=>'同步音色'),
			//array('type'=>'post','url'=>'/agent/updemo/','title'=>'一键生成试听'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'音色标题','type'=>'string'),
			array('name'=>'language','width'=>15,'title'=>'语言','type'=>'radio','options'=>'yuyuantypes'),
		)
	);
	$return['sex']=$this->sex;
	$return['agetype']=$this->agetype;
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aiagent_voice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3,'isnews'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回音色列表'),
		),
		'edit'=>array(
			
			array('name'=>'title','title'=>'音色名称','type'=>'string'),
			array('name'=>'logo','title'=>'音色头像','type'=>'image'),
			array('name'=>'agetype','title'=>'声音成熟','type'=>'radio','options'=>'agetype'),
			array('name'=>'sex','title'=>'男/女','type'=>'radio','options'=>'sex'),
			array('name'=>'desc','title'=>'音色介绍','type'=>'text'),
			array('name'=>'demo','title'=>'试听','type'=>'audio'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['sex']=$this->sex;
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['agetype']=$this->agetype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		//exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写音色名称！','error');
	}
	if(!$postdata['language']){
		exi('请填写语言！','error');
	}
	if(!$postdata['voiceid']){
		exi('请绑定音色！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
		'voiceid'=>trim($postdata['voiceid']),
		'demo'=>trim($postdata['demo']),
		'language'=>trim($postdata['language']),
		'agetype'=>intval($postdata['agetype']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'sex'=>intval($postdata['sex']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aiagent_voice',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aiagent_voice',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择音色！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aiagent_voice',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_aiagent_voice',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$list=$this->model('api')->gettts(1);
	foreach($list as $item){
		$isadd=pdo_get('jy_aiagent_voice',array('language'=>trim($item['language']),'voiceid'=>trim($item['voice_id']),'uniacid'=>$_SESSION['uniacid']));
		$num = mt_rand(1, 6);
		$item['sex']=$isadd['sex'];
		$item['logo']=$item['logo']?:$isadd['logo'];
		$item['logo']=$item['logo']?:'../addons/jy_aiagent/resource/images/voice'.$num.'.png';
		if(!$item['sex'] && strpos($item['voice_name'],'男')!== false){
			$item['sex']=1;
		}
		$data=array(
			'title'=>trim($item['voice_name']),
			'logo'=>trim($item['logo']),
			'desc'=>trim($item['desc']),
			'voiceid'=>trim($item['voice_id']),
			'demo'=>trim($item['voice_demo']),
			'agetype'=>intval($item['agetype']),
			'language'=>trim($item['language']),
			'sex'=>trim($item['sex'])?:2,
			'status'=>1,
			'ishot'=>$item['top']?1:0,
		);
		$id=intval($isadd['id']);
		if($id){
			unset($data['ishot']);
			unset($data['status']);
			unset($data['sex']);
			unset($data['desc']);
			unset($data['agetype']);
			pdo_update('jy_aiagent_voice',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_aiagent_voice',$data);
		}
	}
	exi('同步成功！');
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('zhubo')->daoru($postdata);
		exi('导入成功','','/agent/');
	}
	$return['item']['excelurl']=SITEROOT.'/addons/jy_aiagent/resource/agent.xls';
	//$return['item']['excelurl']='请直接下载模板选择导入！';
	$htmls=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</red></br>
				功能介绍：可将本模块导出的XLSX文件快速上传,节约您的大量时间!</br>
				1、导入时会匹配【姓名】判断重复；</br>
				2、导入时会匹配【voice参数值】判断重复；</br>
				3、导入时会匹配【类型】，如果不存在，将自动添加该分类类型；</br>
				4、重复的会自动忽略；</br>
				5、注意<red>【姓名】，【voice参数值】</red>的准确性！
				
				'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'XLSX示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传XLSX','help'=>''),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aiagent_voice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}